package nl.utwente.eemcs.decomposition;

import nl.utwente.eemcs.common.transformation.IDecomposition;
import nl.utwente.eemcs.decomposition.transformation.DataEdgeValidator;
import nl.utwente.eemcs.decomposition.transformation.DataEdgeValidatorVariableSharing;
import nl.utwente.eemcs.decomposition.transformation.Decomposer;
import nl.utwente.eemcs.graph.Graph;
import nl.utwente.eemcs.graph.GraphCollaboration;
import nl.utwente.eemcs.graph.distribution.DistributionList;
import nl.utwente.eemcs.graphviz.util.Debugger;



public class Decomposition implements IDecomposition {

	private boolean dataVariableValidation = true;
	
	public void useVariableValidation(boolean bool) {
		dataVariableValidation = bool;
	}
	
	@Override
	public GraphCollaboration transform(Graph inputGraph,
			DistributionList distributionList) throws Exception {
		/**
		 * Step 1: Create a collaborative model with all separate processes and communication edges between them 
		 */	
		GraphCollaboration collaborativeModel = Decomposer.performDecomposition(inputGraph, distributionList);
		
		// Convert the graph to a GraphViz image
		Debugger.drawImage(collaborativeModel, "decomposition_collaborativeModel");
		
		/**
		 * Step 2: Check if data constraints are violated
		 */
		if (dataVariableValidation) {
			DataEdgeValidatorVariableSharing.analyze(collaborativeModel, distributionList, false);
		
			// Convert the graph to a GraphViz image
			Debugger.drawImage(collaborativeModel, "decomposition_dataRestrictionValidated");
		} else {
			DataEdgeValidator.analyze(collaborativeModel, distributionList);
		}
		
		/**
		 * Step 3: Optional optimization phase, use one of the optimization strategies
		 */
		return collaborativeModel;
	}

}